package com.dianrui.moonfire.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.dianrui.moonfire.Constant;
import com.dianrui.moonfire.R;
import com.dianrui.moonfire.adapter.HelpCateAdapter;
import com.dianrui.moonfire.adapter.HelpListAdapter;
import com.dianrui.moonfire.bean.HelpCate;
import com.dianrui.moonfire.bean.HelpList;
import com.dianrui.moonfire.okhttp.XHttpRequest;
import com.dianrui.moonfire.util.StringUtils;
import com.dianrui.moonfire.util.ToastUtil;
import com.dianrui.moonfire.util.Url;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
/**
 * Description:帮助中心
 * User: luoziyin
 * Date: 2020-8-17
 */
@SuppressWarnings("ALL")
public class HelpActivity extends BaseActivity {
    private List<HelpCate> cateList = new ArrayList<>();
    private List<HelpList> cateHelpListAll = new ArrayList<>();
    private HelpCateAdapter helpCateAdapter;
    private HelpListAdapter helpListAdapter;
    private String cateId = "1";
    private int pageIndex=1;
    private boolean hasChecked = false;

    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.help_grid_cate)
    GridView helpGridCate;
    @BindView(R.id.help_list)
    ListView helpList;
    @BindView(R.id.tel)
    Button tel;
    @BindView(R.id.emptyView)
    LinearLayout emptyView;

    @Override
    public int getContentViewId() {
        return R.layout.help;
    }

    @Override
    public void initView() {
        customInit(true);
        title.setText(getString(R.string.my_txt4));
        getHelpCate();
        getHelpList();
        helpCateAdapter = new HelpCateAdapter(HelpActivity.this, cateList);
        helpGridCate.setAdapter(helpCateAdapter);
        helpListAdapter = new HelpListAdapter(HelpActivity.this, cateHelpListAll);
        helpList.setAdapter(helpListAdapter);

        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("service_tel"))) {
            tel.setText(getString(R.string.help_tel)+SPUtils.getInstance().getString("service_tel"));
        }

    }


    //获取帮助分类
    private void getHelpCate() {
        try {
            XHttpRequest.getInstance().postRequests(Url.HELP_CATE, "",  new XHttpRequest.XCallBack() {
                @Override
                public void onResponse(String result) {
                    if (!StringUtils.isEmpty(result)) {
                        try {
                            final JSONObject jsonObject = new JSONObject(result);
                            if (null != jsonObject &&!isFinishing()) {
                                if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                    JSONArray jsonArray = jsonObject.optJSONArray("data");
                                    if (jsonArray.length() > 0) {
                                        for (int i = 0; i < jsonArray.length(); i++) {
                                            HelpCate helpCate = new HelpCate();
                                            helpCate.cate_id = jsonArray.optJSONObject(i).optString("cate_id");
                                            helpCate.logo = jsonArray.optJSONObject(i).optString("logo");
                                            helpCate.name = jsonArray.optJSONObject(i).optString("name");
                                            cateList.add(helpCate);
                                        }
                                        if (helpCateAdapter != null) {
                                            helpCateAdapter.refresh(cateList);
                                        }

                                    }

                                }

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override
                public void failedRequest(String result) {
                    ToastUtil.showToast(getString(R.string.net_error));
                }
            });
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

    }

    //帮助中心热门问题
    private void getHelpList() {
        JsonObject jsonObject = new JsonObject();
        if (!StringUtils.isEmpty(SPUtils.getInstance().getString("member_id"))) {
            jsonObject.addProperty("member_id", SPUtils.getInstance().getString("member_id"));
        }
        jsonObject.addProperty("p", pageIndex);
        jsonObject.addProperty("is_top", 1);
        XHttpRequest.getInstance().postRequests(Url.HELP_LIST, jsonObject.toString(), new XHttpRequest.XCallBack() {

            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject &&!isFinishing()) {
                            if (Constant.DATASUCCESS.equals(jsonObject.optString("status"))) {
                                if (cateHelpListAll != null) {
                                    cateHelpListAll.clear();
                                }
                                JSONArray jsonArray = jsonObject.optJSONArray("data");
                                if (jsonArray.length() > 0) {
                                    for (int i = 0; i < jsonArray.length(); i++) {
                                        HelpList helpList = new HelpList();
                                        helpList.article_id = jsonArray.optJSONObject(i).optString("article_id");
                                        helpList.title = jsonArray.optJSONObject(i).optString("title");
                                        cateHelpListAll.add(helpList);
                                    }
                                    if (helpListAdapter != null) {
                                        helpListAdapter.refresh(cateHelpListAll);
                                    }

                                }

                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                if (cateHelpListAll.size() <= 0) {
                    emptyView.setVisibility(View.VISIBLE);
                    helpList.setVisibility(View.GONE);
                } else {
                    emptyView.setVisibility(View.GONE);
                    helpList.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void failedRequest(String result) {
                ToastUtil.showToast(getString(R.string.net_error));
            }
        });
    }


    @Override
    public void initData() {

    }


    @OnClick({R.id.back, R.id.tel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tel:
                if (!StringUtils.isEmpty(SPUtils.getInstance().getString("service_tel"))) {
                    callPhone(SPUtils.getInstance().getString("service_tel"));
                }
                break;
                default:
                    break;

        }
    }

    public void callPhone(String phoneNum) {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ContextCompat.checkSelfPermission(HelpActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(Intent.ACTION_CALL);
                Uri data = Uri.parse("tel:" + phoneNum);
                intent.setData(data);
                startActivity(intent);
            } else {
                ActivityCompat.requestPermissions(HelpActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
            }
        } else {
            Intent intent = new Intent(Intent.ACTION_CALL);
            Uri data = Uri.parse("tel:" + phoneNum);
            intent.setData(data);
            startActivity(intent);
        }

    }


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    if (!StringUtils.isEmpty(SPUtils.getInstance().getString("service_tel"))) {
                        callPhone(SPUtils.getInstance().getString("service_tel"));
                    }
                }
                break;
            default:
                break;

        }

    }

}
